﻿using Dapper;
using HR.IServices.PengIService;
using HR.Models.PengModels;
using System.Data;
using System.Data.SqlClient;

namespace HR.Services.PengService
{
    public class LuYongGuanLiServices:ILuYongGuanLiServices
    {
        private readonly IConfiguration conf;
        private readonly string? conStr;

        public LuYongGuanLiServices(IConfiguration conf)
        {
            this.conf = conf;
            conStr = this.conf["ConnectionStrings"];
        }
        public Task<FenYe<JianLiGuanLi>> CheckFind(int size, int currentPage)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    DynamicParameters dynamicParameters = new DynamicParameters();
                    dynamicParameters.Add("@pageSize", size);
                    dynamicParameters.Add("@keyName", "res_id");
                    dynamicParameters.Add("@tabelName", "engage_resume");
                    dynamicParameters.Add("@where", "state=2");
                    dynamicParameters.Add("@currentPage", currentPage);
                    dynamicParameters.Add("@lie", "*");
                    dynamicParameters.Add("@rows", dbType: DbType.Int32, direction: ParameterDirection.Output);
                    string sql = "exec proc_Fenye @pageSize, @keyName, @tabelName, @where, @currentPage, @lie, @rows out";
                    List<JianLiGuanLi> list = con.Query<JianLiGuanLi>(sql, dynamicParameters).ToList();
                    int rows = dynamicParameters.Get<int>("@rows");
                    FenYe<JianLiGuanLi> fenYe = new FenYe<JianLiGuanLi>()
                    {
                        Rows = rows,
                        JianLiGuanLis = list
                    };
                    return fenYe;
                }
            });
        }

        public Task<FenYe<JianLiGuanLi>> FindState(int size, int currentPage)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    DynamicParameters dynamicParameters = new DynamicParameters();
                    dynamicParameters.Add("@pageSize", size);
                    dynamicParameters.Add("@keyName", "res_id");
                    dynamicParameters.Add("@tabelName", "engage_resume");
                    dynamicParameters.Add("@where", "state=1");
                    dynamicParameters.Add("@currentPage", currentPage);
                    dynamicParameters.Add("@lie", "*");
                    dynamicParameters.Add("@rows", dbType: DbType.Int32, direction: ParameterDirection.Output);
                    string sql = "exec proc_Fenye @pageSize, @keyName, @tabelName, @where, @currentPage, @lie, @rows out";
                    List<JianLiGuanLi> list = con.Query<JianLiGuanLi>(sql, dynamicParameters).ToList();
                    int rows = dynamicParameters.Get<int>("@rows");
                    FenYe<JianLiGuanLi> fenYe = new FenYe<JianLiGuanLi>()
                    {
                        Rows = rows,
                        JianLiGuanLis = list
                    };
                    return fenYe;
                }
            });
        }

        public Task<FenYe<JianLiGuanLi>> PassFind(int size,int currentPage)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    DynamicParameters dynamicParameters = new DynamicParameters();
                    dynamicParameters.Add("@pageSize", size);
                    dynamicParameters.Add("@keyName", "res_id");
                    dynamicParameters.Add("@tabelName", "engage_resume");
                    dynamicParameters.Add("@where", "check_status=3");
                    dynamicParameters.Add("@currentPage", currentPage);
                    dynamicParameters.Add("@lie", "*");
                    dynamicParameters.Add("@rows", dbType: DbType.Int32, direction: ParameterDirection.Output);
                    string sql = "exec proc_Fenye @pageSize, @keyName, @tabelName, @where, @currentPage, @lie, @rows out";
                    List<JianLiGuanLi> list = con.Query<JianLiGuanLi>(sql, dynamicParameters).ToList();
                    int rows = dynamicParameters.Get<int>("@rows");
                    FenYe<JianLiGuanLi> fenYe = new FenYe<JianLiGuanLi>()
                    {
                        Rows = rows,
                        JianLiGuanLis = list
                    };
                    return fenYe;
                }
            });
        }

        public Task<int> ShenHeYiJian(JianLiGuanLi jianLiGuanLi)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr)) 
                {
                    return con.Execute($"update engage_resume set pass_checkComment='{jianLiGuanLi.Pass_checkComment}',state='{jianLiGuanLi.Pass_check_status}' where res_id='{jianLiGuanLi.Res_id}'");
                }
            });
        }

        public Task<int> ShenPiYiJian(JianLiGuanLi jianLiGuanLi)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Execute($"update engage_resume set pass_passComment='{jianLiGuanLi.Pass_passComment}',state='{jianLiGuanLi.Pass_check_status}' where res_id='{jianLiGuanLi.Res_id}'");
                }
            });
        }
    }
}
